﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Gun : MonoBehaviour {

    public int attack = 50;
    public float fireRate = 1;
    public float range = 10;
    int ammo = 0;
    public int Ammo
    {
        get { return ammo; }
        set 
        {
            ammo = value;
            ammo = Mathf.Clamp(ammo, 0, magAmmo);
        }
    }

    public int magAmmo = 12;
    public float reloadTime = 1;
    public Transform firePoint;
    public List<Collider> enemies;
    public Transform target;
    public LayerMask whatIsEnemy;

    protected float cooldown = 0;
    protected float timer = 0;
    protected bool canFire = true;
    protected bool isReloading = false;

    public virtual void Awake()
    {
        cooldown = 1 / fireRate;
        timer = 0;
        ammo = magAmmo;
    }

    public virtual void Update()
    {
        if (isReloading)
        {
            Reload();
        }
        else
        {
            CheckAmmo();
            CheckCanFire();
        }

        if (!target)
        {
            FindTarget();
        }
        else
        {
            Aim();
        }
    }

    public virtual void FixedUpdate()
    {
        if (canFire)
        {
            Fire();
        }
    }

    public virtual void Reload()
    {
        Debug.Log("Reloading");
        canFire = false;
        timer += Time.deltaTime;
        if (timer >= reloadTime)
        {
            timer = 0;
            ammo = magAmmo;
            isReloading = false;
            canFire = true;
        }
    }

    public virtual void CheckAmmo()
    {
        if (ammo <= 0)
        {
            isReloading = true;
            timer = 0;
            canFire = false;
            //return;
        }
    }

    public virtual void CheckCanFire()
    {
        if (canFire)
        {
            timer = 0;
        }
        else
        {
            timer += Time.deltaTime;
            if (timer >= cooldown)
            {
                timer = 0;
                canFire = true;
            }
        }
    }

    public virtual void FindTarget()
    {
        Collider[] temp = Physics.OverlapSphere(transform.position, range, whatIsEnemy);

        if (temp.Length > 0)
        {
            // Duyet qua ca danh sach
            for (int i = 0; i < temp.Length; i++)
            {
                if (!enemies.Contains(temp[i])) // Neu chua co trong danh sach thi them vao
                {
                    enemies.Add(temp[i]);
                }
            }

            if (enemies.Count > 0)
            {
                for (int j = 0; j < enemies.Count; j++)
                {
                    if (enemies[j].tag == "Shootable")
                    {
                        target = enemies[j].transform;
                        break;
                    }
                    else
                    {
                        enemies.RemoveAt(j);
                        j--;
                    }
                }
            }
        }

       
    }

    public virtual void Aim()
    {
        if (target)
        {
            Vector3 temp = target.position;
            temp.y = firePoint.position.y;
            transform.LookAt(temp);

            if (target.GetComponent<Character>().HP <= 0)
                target = null;
        }
    }

    public virtual void Fire(){ }

    void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(transform.position, range);
    }
}
